import Vue from 'vue'
import VueRouter from 'vue-router'
import { getUserId } from '@/utils/getUserId';

// 解决编程式路由往同一地址跳转时会报错的情况
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;

// push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch(err => err);
};

//replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalReplace.call(this, location, onResolve, onReject);
  return originalReplace.call(this, location).catch(err => err);
};



Vue.use(VueRouter)

const routes = [
  // 路由重定向，当页面路径为空时，跳转登录界面
  {
    path:'/',
    redirect:'/login'
  },
  // 登录
  {
    path:'/login',
    name:'Login',
    component:()=>import('../views/Login.vue')
  },
  // 注册
  {
    path: '/register',
    name:'Register',
    component:()=>import('../views/Register.vue')
  },
  // 人工客服 (跳转打开新页面)
  {
    path:'/service',
    name:'service',
    component:()=>import('../views/Service.vue')
  },
  // 关于我们
  {
    path:'/about_us',
    name:'about_us',
    component:()=>import('../views/AboutUs.vue')
  },
  {
    path:'/404',
    name:'404',
    component:()=>import('../views/404.vue')
  },
  // 前台商城
  {
    path:'/mall',
    name:'Mall',
    // meta:{title:'光华商城'},
    redirect:'/mall/first',
    component:()=>import('../views/shop/Mall.vue'),
    children:[
      {path:'first',name:'first',component:()=>import('../views/shop/views/First.vue')},
      {path:'category',name:'product',component:()=>import('../views/shop/views/Category.vue')},
      {path:'detail',name:'detail',component:()=>import('../views/shop/views/Detail.vue')},
      {path:'cart',name:'cart',component:()=>import('../views/shop/views/Cart.vue')},
      {path:'my_order',name:'myorder',component:()=>import('../views/shop/views/MyOrder.vue')}
    ]
  },
  
  // 后台管理
  {
    path: '/manage',
    name: 'Manage',
    component: ()=>import("../views/Manage.vue"),
    children:[
      { path:'home', name:'首页', component:()=>import("../views/Home")},
      { path:'user', name:'用户列表', component:()=>import("../views/User")},
      { path:'person',name:'个人信息',component:()=>import("../views/Person")},
      // 权限管理
      { path:'role_list',name:'角色列表',component:()=>import('../views/authority/RoleList.vue')},
      { path:'authority_divide',name:'权限分配',component:()=>import('../views/authority/AuthorityDivide.vue')},
      // 商品管理
      { path:'goods_list',name:'商品列表',component:()=>import('../views/goods/GoodsList.vue')},
      { path:'goods_cate',name:'商品分类',component:()=>import('../views/goods/GoodsCate.vue')},
      // 订单管理
      { path:'order_list',name:'订单列表',component:()=>import('../views/orders/OrderList.vue')},
      // 数据统计
      { path:'data_statistics',name:'数据统计',component:()=>import('../views/datas/DataStatistics.vue')},
      //文件管理
      { path:'file',name:'文件管理',component:()=>import('../views/File.vue')}
    ]
  },
  
  
  
]

const router = new VueRouter({
  linkExactActiveClass: 'active',
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach(async (to,from,next)=>{
  document.body.scrollTop = 0
  // firefox
  document.documentElement.scrollTop = 0
  localStorage.setItem("currentPathName",to.name)
  document.title='光华商城'
  // console.log('from:',from);
  // console.log('to:',to);
  if(to.path.includes('manage')){
    if(from.path.includes('login') || from.path.includes('manage')){
      console.log('无需权限验证');
      next()
    }else{
      let token=JSON.parse(sessionStorage.getItem('user')).token
      if(token){
        let id =getUserId(token)
        let res=await Vue.prototype.request.get('/user/'+`${id}`)
        if(res.data.role=='管理员'){
          console.log('管理员可以访问该界面');
          // console.log('你正在访问后台管理页面');
          next()
        }else{
          Vue.prototype.$message.warning('你不是管理员，无权访问')
          console.log('你不是管理员，无权访问');
          next({path:'/mall'})
        }
      }else{
        console.log('取不到token');
      }
    }
    
    
  }else{
    next()
  } 
})
export default router
